package com.fbs.job.admin.controller;

import com.fbs.job.admin.core.annotation.PermissionLimit;
import com.fbs.job.admin.core.entity.FbsJobActuator;
import com.fbs.job.admin.mapper.FbsJobInfoMapper;
import com.fbs.job.admin.service.FbsJobActuatorService;
import com.fbs.job.core.model.ResultT;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/job_actuator")
public class JobActuatorController {

    @Autowired
    private FbsJobActuatorService fbsJobActuatorService;
    @Resource
    private FbsJobInfoMapper fbsJobInfoMapper;


    @RequestMapping
    @PermissionLimit(adminuser = true)
    public String index(Model model) {
        return "actuator/actuator";
    }


    @RequestMapping("/getById")
    @ResponseBody
    @PermissionLimit(adminuser = true)
    public ResultT<FbsJobActuator> getById(int id){
        FbsJobActuator jobActuator = fbsJobActuatorService.getById(id);
        return jobActuator!=null? ResultT.success(jobActuator): ResultT.error(null);
    }


    @RequestMapping("/add")
    @PermissionLimit(adminuser = true)
    public String add(HttpServletRequest request,Model model) {
        String id = request.getParameter("id");
        if (StringUtils.isNotBlank(id)){
            FbsJobActuator actuator = fbsJobActuatorService.getById(Integer.parseInt(id));
            model.addAttribute("actuator",actuator);
        }else{
            model.addAttribute("actuator",new FbsJobActuator());
        }
        return "actuator/add";
    }

    @RequestMapping("/pageList")
    @ResponseBody
    @PermissionLimit(adminuser = true)
    public Map<String, Object> pageList(HttpServletRequest request,
                                        @RequestParam(required = false, defaultValue = "0") int page,
                                        @RequestParam(required = false, defaultValue = "10") int rows,
                                        String appName, String title) {
  					// 分页列表
        return fbsJobActuatorService.pageList(request,page,rows,appName,title);
    }



    @PostMapping("/addActuator")
    @ResponseBody
    public ResultT<String> addActuator(@RequestBody FbsJobActuator fbsJobActuator){
        return  fbsJobActuatorService.addActuator(fbsJobActuator);
    }




    @RequestMapping("/delete")
    @ResponseBody
    @PermissionLimit(adminuser = true)
    public ResultT<String> delete(String id){

        // valid
        int count = fbsJobInfoMapper.pageListCount(0, 10, Integer.parseInt(id), -1,  null, null, null);
        if (count > 0) {
            return new ResultT<String>(500,"拒绝删除，该执行器使用中");
        }

        List<FbsJobActuator> allList = fbsJobActuatorService.findAll();
        if (allList.size() == 1) {
            return new ResultT<>(500, "拒绝删除, 系统至少保留一个执行器" );
        }

        int ret = fbsJobActuatorService.delete(Integer.parseInt(id));
        return (ret>0)?ResultT.success():ResultT.error("删除失败");
    }



}
